<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class goods_act_uqcms extends control
{
    public function seckill()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $status = isset($_GET['status']) ? $_GET['status'] : '';
        $wsql = '';
        if ($status) {
            $wsql = ' status = ' . $status . ' ';
        } elseif ($status == '0') {
            $wsql = ' status = 0 ';
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $result = $this->db->get_all("select * from " . table('promote_seckill') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $v) {
                $gid[] = $v['gid'];
            }
            if ($gid) {
                $gid_in = implode(',', $gid);
                $garr = $this->db->get_all("select id,title,images,price from " . table('goods') . " where id in (" . $gid_in . ")");
                foreach ($garr as $v) {
                    $garr[$v['id']] = $v;
                }
                foreach ($result as $v) {
                    $v['images'] = $garr[$v['gid']]['images'];
                    $v['title'] = $garr[$v['gid']]['title'];
                    $v['price'] = $garr[$v['gid']]['price'];
                    $v['start_time'] = _date($v['start_time']);
                    $v['end_time'] = _date($v['end_time']);
                    $v['addtime'] = _date($v['addtime']);
                    $list[] = $v;
                }
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('promote_seckill') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function seckill_status()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : exit('error');
        $type = $_POST['type'];
        $this->db->query("update " . table('promote_seckill') . " set {$type} = ({$type} + 1)%2 where id = {$id} limit 1");
        $row = $this->db->get_one("select * from " . table('promote_seckill') . " where id = {$id}");
        echo $row[$type];
    }
    function seckill_del()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : error_json('提交错误');
        $q1 = $this->db->delete(table('promote_seckill'), "id = " . $id);
        $q2 = $this->db->delete(table('promote_seckill_sku'), 'sid = ' . $id);
        if ($q1 && $q2) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
    public function reduce()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $status = isset($_GET['status']) ? $_GET['status'] : '';
        $wsql = '';
        if ($status) {
            $wsql = ' status = ' . $status . ' ';
        } elseif ($status == '0') {
            $wsql = ' status = 0 ';
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $result = $this->db->get_all("select * from " . table('promote_reduce') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $v) {
                $idarr[] = $v['shop_id'];
            }
            if ($idarr) {
                $id_in = implode(',', $idarr);
                $sparr = $this->db->get_all("select id,name from " . table('shop') . " where id in (" . $id_in . ")");
                foreach ($sparr as $v) {
                    $sparr[$v['id']] = $v;
                }
                foreach ($result as $v) {
                    $v['start_time'] = _date($v['start_time']);
                    $v['end_time'] = _date($v['end_time']);
                    $v['addtime'] = _date($v['addtime']);
                    $v['shop_name'] = $sparr[$v['shop_id']]['name'];
                    $list[] = $v;
                }
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('promote_reduce') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function reduce_del()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : error_json('提交错误');
        $q1 = $this->db->delete(table('promote_reduce'), "id = " . $id);
        if ($q1) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
    public function group()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $status = isset($_GET['status']) ? $_GET['status'] : '';
        $wsql = '';
        if ($status) {
            $wsql = ' status = ' . $status . ' ';
        } elseif ($status == '0') {
            $wsql = ' status = 0 ';
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $result = $this->db->get_all("select * from " . table('promote_group') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $v) {
                $spid_arr[] = $v['shop_id'];
                $gid_arr[] = $v['gid'];
            }
            if (isset($gid_arr)) {
                $gid_in = implode(',', $gid_arr);
                $gs_query = $this->db->get_all("select * from " . table('goods') . " where id in (" . $gid_in . ")");
                foreach ($gs_query as $v) {
                    $gsarr[$v['id']] = $v;
                }
            }
            if ($spid_arr) {
                $spid_in = implode(',', $spid_arr);
                $sparr = $this->db->get_all("select id,name from " . table('shop') . " where id in (" . $spid_in . ")");
                foreach ($sparr as $v) {
                    $sparr[$v['id']] = $v;
                }
            }
            foreach ($result as $v) {
                $v['start_time'] = _date($v['start_time']);
                $v['end_time'] = _date($v['end_time']);
                $v['addtime'] = _date($v['addtime']);
                $v['shop_name'] = $sparr[$v['shop_id']]['name'];
                $v['title'] = $gsarr[$v['gid']]['title'];
                $v['images'] = $gsarr[$v['gid']]['images'];
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('promote_reduce') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function group_status()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : exit('error');
        $type = $_POST['type'];
        $this->db->query("update " . table('promote_group') . " set {$type} = ({$type} + 1)%2 where id = {$id} limit 1");
        $row = $this->db->get_one("select * from " . table('promote_group') . " where id = {$id}");
        echo $row[$type];
    }
    function group_del()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : error_json('提交错误');
        $q1 = $this->db->delete(table('promote_group'), "id = " . $id);
        $q2 = $this->db->delete(table('promote_group_sku'), 'group_id = ' . $id);
        if ($q1 && $q2) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
    function group_order()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $status = isset($_GET['status']) ? $_GET['status'] : '';
        $wsql = '';
        if ($status) {
            $wsql = ' status = ' . $status . ' ';
        } elseif ($status == '0') {
            $wsql = ' status = 0 ';
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $cancel_time = cfg('sys_cancel_order_time');
        $oall = $this->db->get_all("select * from " . table('promote_group_order') . $wsql . " order by id desc");
        if ($oall) {
            foreach ($oall as $v) {
                $gid_arr[] = $v['gid'];
            }
            $gid_in = implode(',', $gid_arr);
            $gall = $this->db->get_all("select id,images,title from " . table('goods') . " where id in(" . $gid_in . ")");
            if ($gall) {
                foreach ($gall as $v) {
                    $goods[$v['id']] = $v;
                }
            }
            foreach ($oall as $v) {
                $v['goods'] = $goods[$v['gid']];
                $v['end_time'] = _date($v['start_time'] + $cancel_time);
                $v['start_time'] = _date($v['start_time']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $page = '';
            $data['page'] = $page;
            $this->assign('data', $data);
        }
        $this->display();
    }
    function group_detail()
    {
        $id = $_GET['id'];
        $rall = $this->db->get_all("select * from " . table('promote_group_relate') . " where group_id = " . $id . " order by addtime asc");
        if ($rall) {
            foreach ($rall as $v) {
                $uid_arr[] = $v['uid'];
            }
            $uid_in = implode(',', $uid_arr);
            $uall = $this->db->get_all("select uid,avatar,mobile from " . table('member') . " where uid in(" . $uid_in . ")");
            if ($uall) {
                foreach ($uall as $v) {
                    $user_arr[$v['uid']] = $v;
                }
            }
            foreach ($rall as $v) {
                $v['avatar'] = $user_arr[$v['uid']]['avatar'];
                $v['mobile'] = $user_arr[$v['uid']]['mobile'];
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $this->assign('data', $data);
        }
        $this->display();
    }
    function group_order_del()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : error_json('提交错误');
        $q1 = $this->db->delete(table('promote_group_order'), "id = " . $id);
        $q2 = $this->db->delete(table('promote_group_relate'), 'group_id = ' . $id);
        if ($q1 && $q2) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
}